package fiszki.namespace;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

public class StartActivity extends Activity {
    private ImageButton back;
    private ListView lvSets;
 
    private SetsDBAdapter setsDBAdapter;
    private Cursor setsCursor;
    private List<SetTasks> tasks;
    private SetTasksAdapter listAdapter;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().format = android.graphics.PixelFormat.RGBA_8888;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.sets);

        initUiElements();
        initListView();
        initButtonsOnClickListeners(); 
	}
	
    private void initUiElements() { 	
        back = (ImageButton) findViewById(R.id.buttonBack);
        lvSets = (ListView) findViewById(R.id.lvSets);
    }
    
    private void initButtonsOnClickListeners() {
    	back.setOnClickListener(new View.OnClickListener() {
            
			@Override
			public void onClick(View v) {
            	finish();
            }
        });
	}
    
    private void initListView() {
        fillListViewData();
        initListViewOnItemClick();
    }
    
    private void fillListViewData() {
        setsDBAdapter = new SetsDBAdapter(getApplicationContext());
        setsDBAdapter.open();
        getAllSets();
        listAdapter = new SetTasksAdapter(this, tasks);
        listAdapter.notifyDataSetChanged();
        lvSets.setAdapter(listAdapter);
    }
     
    private void getAllSets() {
        tasks = new ArrayList<SetTasks>();
        setsCursor = getAllEntriesFromDB();
        
        updateSetsList();
    }
     
    // OK
    private Cursor getAllEntriesFromDB() {
        setsCursor = setsDBAdapter.getAllSets();
        
        if(setsCursor != null) {
            startManagingCursor(setsCursor);
            setsCursor.moveToFirst();
        }
        
        return setsCursor;
    }
     
    private void updateSetsList() {
        if(setsCursor != null && setsCursor.moveToFirst()) {
            do {
                int id = setsCursor.getInt(SetsDBAdapter.ID_COLUMN);
                String name = setsCursor.getString(SetsDBAdapter.NAME_COLUMN);
                String questionLang = setsCursor.getString(SetsDBAdapter.QUESTIONLANG_COLUMN);
                String answerLang = setsCursor.getString(SetsDBAdapter.ANSWERLANG_COLUMN);
                
                tasks.add(new SetTasks(id, name, questionLang, answerLang));
            } 
            while(setsCursor.moveToNext());
        }
    }
    
    private void initListViewOnItemClick() {
        lvSets.setOnItemClickListener(new OnItemClickListener() {
            
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            	SetTasks task = tasks.get(position);
            	Bundle setID = new Bundle();
            	setID.putInt("setID", task.getId());
            	Intent newIntent = new Intent(StartActivity.this, LearningActivity.class);
            	newIntent.putExtras(setID);
            	startActivityForResult(newIntent, 0);
            }
        });
    }
    
    @Override
    protected void onDestroy() {
        if(setsDBAdapter != null)
            setsDBAdapter.close();
        
        super.onDestroy();
    }
}
